<template>
	<view class="collection">
		<view class="tips">向左滑动移除收藏</view>
		<view class="container">
			<view class="content-card" v-if="collectionList.length > 0">
				<uni-swipe-action>
					<uni-swipe-action-item v-for="(item, index) in collectionList" :key="index" :right-options="options"
						@click="bindClick(item.id)">
						<view class="goods-item" v-if="item.type===0"
							@click="pageRoute('/pages/goods-detail/goods-detail?id='+item.goods.id)">
							<image class="thumb" :src="item.goods.logo" mode="widthFix"></image>
							<view class="info">
								<view class="name">{{item.goods.name}}</view>
								<view class="sale-num">已售{{item.goods.sales_sum}}</view>
								<view class="price">￥{{item.goods.sale_price}}</view>
							</view>
						</view>

						<view class="goods-item" v-else-if="item.type===1"
							@click="pageRoute('/pages/goods-detail/goods-detail?id='+item.store.id)">
							<image class="thumb" :src="item.store.logo" mode="widthFix"></image>
							<view class="info">
								<view class="name">{{item.store.name}}</view>
								<view class="sale-num">已服务{{item.store.sales_sum}}</view>
								<!-- <view class="price">￥{{item.goods.sale_price}}</view> -->
							</view>
						</view>

						<view class="goods-item" v-else-if="item.type===2"
							@click="pageRoute('/pages/goods-detail/goods-detail?id='+item.technician.id)">
							<image class="thumb" :src="item.technician.avatar" mode="widthFix"></image>
							<view class="info">
								<view class="name">{{item.technician.name}}</view>
								<view class="sale-num">已服务{{item.technician.sales_sum}}</view>
								<view class="price">评分{{item.technician.score}}</view>
							</view>
						</view>

						<view class="goods-item" v-else
							@click="pageRoute('/pages/goods-detail/goods-detail?id='+item.service.id)">
							<image class="thumb" :src="item.service.images" mode="widthFix"></image>
							<view class="info">
								<view class="name">{{item.service.name}}</view>
								<view class="sale-num">已售{{item.service.sales_sum}}</view>
								<view class="price">￥{{item.service.sale_price}}</view>
							</view>
						</view>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</view>
			<view class="content-card" style="text-align: center;padding: 10px 0px;" v-else>
				暂无数据
			</view>
		</view>
	</view>
</template>

<script setup>
	import request from '@/utils/request';
	import checkLogin from '@/utils/login';
	import {
		ref,
		reactive,
		toRefs,
		inject
	} from 'vue';
	import {
		onLoad,
		onShow,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app';

	const state = reactive({
		options: [{
			text: '移除',
			style: {
				fontSize: '28rpx',
				backgroundColor: '#dd524d'
			}
		}],
		collectionList: []
	});

	onLoad((opt) => {

	});

	onShow(() => {
		//登录过期自动登录
		if (!checkLogin()) {
			pageRoute('/pages/users/login/login');
		} else {
			getCollectionList();
		}
	})

	onPullDownRefresh((e) => {
		console.log("下拉刷新");
	})

	onReachBottom((e) => {
		console.log("上拉加载更多", state.isMore);
		if (state.isMore) {
			getCollectionList();
		}
	})

	function tabChange(e) {
		state.current = e.currentIndex;
	}

	function getCollectionList() {
		request.get("collection/index", {
			skip: state.collectionList.length,
			limit: 10,
		}).then(res => {
			console.log('收藏列表', res);
			state.isMore = res.data.is_more;
			if (state.collectionList.length == 0) {
				state.collectionList = res.data.data;
			} else {
				state.collectionList = state.collectionList.concat(res.data.data);
			}
		})
	}

	function bindClick(id) {
		console.log('移除收藏', id);
		request.get("collection/del", {
			id: id,
		}).then(res => {
			uni.showToast({
				title: res.msg
			})
			getCollectionList();
		})
	}

	function pageRoute(url) {
		uni.navigateTo({
			url
		});
	}

	const {
		options,
		collectionList
	} = toRefs(state);
</script>

<style lang="scss">
	page,
	.collection {
		height: 100%;
	}

	.collection {
		.tips {
			padding: $margin-12 $margin-24;
			background-color: mix($inverse-color, $warning-color, 95%);
			font-size: $font-24;
			color: $warning-color;
		}

		.container {
			padding: $margin-24;

			.content-card {
				min-height: calc(100vh - 128rpx);
				background-color: $bg-inverse-color;
				border-radius: $radius-12;

				.store-item,
				.goods-item {
					@include f-between;
					padding: $margin-24;
					border-bottom: 1rpx solid mix($inverse-color, $border-color, 45%);

					.logo,
					.thumb {
						width: 128rpx;
						height: 128rpx;
						background-color: $bg-color;
						border-radius: $radius-8;
					}

					.info {
						flex: 1;
						margin-left: $margin-24;

						.name {
							@include ellipsis;
							color: $main-color;
							font-weight: bold;
						}
					}
				}

				.store-item {
					.info {
						.other {
							display: flex;
							align-items: center;
							margin-top: $margin-12;
							font-size: $font-24;

							.score {
								@include f-center;
								line-height: 1;
								color: $warning-color;
							}

							.sale-num {
								margin-left: $margin-12;
								font-size: $font-24;
								color: $tips-color;
							}
						}

						.address {
							margin-top: $margin-12;
							font-size: $font-24;
							color: $tips-color;
							font-weight: lighter;
						}
					}
				}

				.goods-item {
					.info {
						.sale-num {
							margin-top: $margin-12;
							font-size: $font-24;
							color: $tips-color;
						}

						.price {
							margin-top: $margin-12;
							color: $error-color;
						}
					}
				}
			}
		}
	}
</style>